package com.lh0811.forge.dependency.security.client.component.annotation;

import com.lh0811.forge.dependency.security.client.service.SecurityClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;

/**
 * @author lh0811
 * @date 2021/5/10
 */
@Aspect
@Slf4j
@Component
public class TokenToAuthorityAop extends AuthAbstractClass implements Ordered {

    @Resource
    private SecurityClient securityClient;

    @Pointcut("@within(com.lh0811.forge.dependency.security.client.component.annotation.TokenToAuthority) || @annotation(com.lh0811.forge.dependency.security.client.component.annotation.TokenToAuthority)")
    public void annotationPointCut() {
    }


    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("进入HasTokenAop切面");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 优先获取方法上的注解
        TokenToAuthority annotation = signature.getMethod().getAnnotation(TokenToAuthority.class);
        if (annotation == null) {
            // 方法上如果没有注解，再获取类上的注解
            Annotation declaredAnnotation = signature.getDeclaringType().getDeclaredAnnotation(TokenToAuthority.class);
            if (declaredAnnotation != null) {
                annotation = (TokenToAuthority) declaredAnnotation;
            }
        }
        securityClient.tokenToAuthority();
        Object proceed = joinPoint.proceed(joinPoint.getArgs());
        log.info("退出HasTokenAop切面");
        return proceed;
    }

    @Override
    public int getOrder() {
        return 0;
    }


}
